package com.qqtech.qquan.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.sys.dao.SysAreaDao;
import com.qqtech.qquan.sys.model.SysAreaQuery;
import com.qqtech.qquan.sys.model.SysAreaVo;
import com.qqtech.qquan.user.dao.UserAddressDao;
import com.qqtech.qquan.user.model.UserAddress;

@Service
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddress>
		implements UserAddressService {
	public static final Logger log = LoggerFactory.getLogger(UserAddressServiceImpl.class);
	
	@Resource
	private UserAddressDao userAddressDao;
	
	@Resource
	private SysAreaDao sysAreaDao;

	@Override
	protected BaseDao<UserAddress> getBaseDao() {
		return userAddressDao;
	}

	@Override
	public int addUserAddress(UserAddress userAddress) {
		userAddress.setStatus(YesNoEnum.YES.getCode());
		//设置默认地址
		UserAddress defaulAddress = new UserAddress();
		defaulAddress.setIsDefault(YesNoEnum.YES.getCode());
		defaulAddress.setStatus(YesNoEnum.YES.getCode());
		defaulAddress.setCreatorId(userAddress.getCreatorId());
		//UserAddress address = userAddressDao.getOne(userAddress);
		List<UserAddress> userAddressList =  userAddressDao.queryList(defaulAddress);
		if(userAddressList != null && userAddressList.size() > 0){
			userAddress.setIsDefault(YesNoEnum.NO.getCode());
		}else{
			userAddress.setIsDefault(YesNoEnum.YES.getCode());
		}
		int id = userAddressDao.insert(userAddress);
		return id;
	}

	@Override
	public Result handleDefaultAddress(Integer id) {
		UserAddress userAddress = userAddressDao.getById(id);
		userAddressDao.updateIsDefault(userAddress.getCreatorId());
		UserAddress update = new UserAddress();
		update.setId(id);
		update.setIsDefault(YesNoEnum.YES.getCode());
		userAddressDao.updateById(update);
		return null;
	}

	@Override
	public int removeUserAddress(UserAddress userAddress) {
		int id = userAddressDao.updateById(userAddress);
		//如果没有默认地址，则设置最新一条地址为默认地址
		UserAddress defaulAddress = new UserAddress();
		defaulAddress.setIsDefault(YesNoEnum.YES.getCode());
		defaulAddress.setStatus(YesNoEnum.YES.getCode());
		defaulAddress.setCreatorId(userAddress.getCreatorId());
		//UserAddress address = userAddressDao.getOne(defaulAddress);
		List<UserAddress> userAddressList =  userAddressDao.queryList(defaulAddress);
		if(userAddressList != null && userAddressList.size() > 0){
			return id;
		}else{
			UserAddress query = new UserAddress();
			query.setStatus(YesNoEnum.YES.getCode());
			query.setCreatorId(userAddress.getCreatorId());
			//UserAddress temp = userAddressDao.getOne(query, new PageOrder(Direction.DESC, "id"));
			List<UserAddress> list =  userAddressDao.queryList(query);
			if(list != null && list.size() > 0){
				UserAddress temp = list.get(0);
				temp.setIsDefault(YesNoEnum.YES.getCode());
				userAddressDao.updateById(temp);
			}
			
		}

		return id;
	}
	
}